$().ready(function() {

    $('.summernote').summernote({
        height : '300px',
        lang : 'zh-CN',
        toolbar: [
            ['style', ['bold', 'italic', 'underline', 'clear']],
            ['font', ['strikethrough', 'superscript', 'subscript']],
            ['fontsize', ['fontsize']],
            ['fontname', ['fontname']],
            ['color', ['color']],
            ['para', ['ul', 'ol', 'paragraph']],
            ['height', ['height']],
            ['table', ['table']],
            ['insert', ['link', 'picture']],
            ['view', ['fullscreen', 'codeview', 'help']]
        ],
        callbacks: {
            onImageUpload: function(files, editor, $editable) {
                sendFile(files);
            }
        }
    });
    var content = $("#contents").val();
    $('#content_sn').summernote('code', content);
    $("#firstColumn").val($("#firstColumnHidden").val());
    $("#secondColumn").val($("#secondColumnHidden").val());
    $("#state").val($("#stateHidden").val());
	validateRule();
	//判断是否为资料文库，是则隐藏富文本
    show($("#secondColumnHidden").val());

    firstList();
    //分页下拉框
    $("#firstColumnDiv").on("change","#firstColumn",function () {
        sendList($("#firstColumn").val())
    });

    $("#secondColumnDiv").on("change","#secondColumn",function () {
        show($("#secondColumn").val())
    });

});

function show(name) {
    if(name=='tra_information_library' || name=='cer_information_library'
        || name=='cer_exam_notes' || name=='cer_exam_notes' || name=='cer_registration_notice'
        || name=='cer_expert_application_template' || name=='cer_site_application_template' || name=='evaluation_staff_apply_temp'
        || name=='organization_member_apply_temp' || name=='lecturer_apply_temp' || name=='supervisor_apply_temp'
        || name=='check_point_apply_temp'|| name=='evaluation_point_apply_temp' ){
        $("#isContents").hide();
    }else{
        $("#isContents").show();
    }

    if(name=='职业技能等级标准'||name=='教材配套资源' || name=='考核评分标准'
        || name=='考评标准' || name=='课程标准' || name=='操作手册'
        || name=='其他资料'){
        $("#role").show();
    }else{
        $("#role").hide();
    }

}

function sendList(data) {
    $.ajax({
        url: '/jzweb/consultations/dicLists',
        data :{type:data} ,
        success: function (r) {
            var list ='<select id="secondColumn" name="secondColumn" class="form-control">';
            if(r.data.length==0){
                list+='<option value="">暂无数据</option>';
            }else{
                list+='<option value="">请选择</option>';
                for(var p in r.data){
                    if( $("#secondColumnHidden").val()==r.data[p].value){
                        list+='<option value="'+r.data[p].type+'" selected>'+r.data[p].value+'</option>';
                    }else{
                        list+='<option value="'+r.data[p].type+'">'+r.data[p].value+'</option>';
                    }
                }
            }
            list+='</select>';
            $("#secondColumnDiv").html(list);
            console.log(r)
        }
    })
}
function firstList() {
    $.ajax({
        cache : true,
        type : "GET",
        url : "/jzweb/consultations/dicLists",
        data : {type:""} ,
        success: function (r) {
            var list ='<select id="firstColumn" name="firstColumn" class="form-control">';
            if(r.data.length==0){
                list+='<option value="">暂无数据</option>';
            }else{
                list+='<option value="">请选择</option>';
                for(var p in r.data){
                    if( $("#firstColumnHidden").val()==r.data[p].value){
                        list+='<option value="'+r.data[p].type+'" selected>'+r.data[p].value+'</option>';
                    }else{
                        list+='<option value="'+r.data[p].type+'">'+r.data[p].value+'</option>';
                    }
                }
            }
            list+='</select>';
            $("#firstColumnDiv").html(list);
        }
    });
}

$.validator.setDefaults({
	submitHandler : function() {
        $("#contents").val($('.summernote').summernote('code'));
		update();
	}
});

function getCheckedRoles() {
	var adIds = "";
	$("input:checkbox[name=role]:checked").each(function(i) {
		if (0 == i) {
			adIds = $(this).val();
		} else {
			adIds += ("," + $(this).val());
		}
	});
	return adIds;
}

function update() {
    $("#roleIds").val(getCheckedRoles());
	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/consultations/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
        rules : {
            firstColumn : {
                required : true
            },
            secondColumn : {
                required : true
            },
            title : {
                required : true
            },
            // contents : {
            //     required : true
            // }
        },
        messages : {
            firstColumn : {
                required : icon + "请输入一级栏目"
            },
            secondColumn : {
                required : icon + "请输入二级栏目"
            },
            title : {
                required : icon + "请输入标题"
            },
            // contents : {
            //     required : icon + "请输入内容"
            // }
        }
	})
}


layui.use('upload', function () {
    var upload = layui.upload;
    //执行实例
    upload.render({
        elem: '#btnUpload', //绑定元素
        url: '/common/sysFile/upload', //上传接口
        accept: 'file',
        done: function (r) {
            $("#files").val(r.data);
            $("#test").html(subFileName(r.data))
        },
        error: function (r) {
            layer.msg(r.msg);
        }
    });
    upload.render({
        elem: '#btnUpload1',
        url: '/common/sysFile/upload',
        accept: 'images', //只允许上传图片
        acceptMime: 'image/*', //只筛选图片
        done: function (r) {
            $("#thumbnail").val(r.data);
            $("#img").attr("src",r.data)
        },
        error: function (r) {
            layer.msg(r.msg);
        }
    });
});